#include "../src/Uart3.h"
#include "../src/Uart5.h"
#include "../src/Common.h"
#include "../src/Debug.h"

#define PRINT_CHAR_TO_PANEL_OR_RF   (0)
#define CHARBUF_LEN (256)
#define MAX_CHAR_SENT_ONCE_LEN  (64)

uint8_t CharBuf[CHARBUF_LEN];
uint8_t CharBufTxHead = 0;
uint8_t CharBufTxTail = 0;

PanelCommDebugPack PanelCommDebugPacket;


void PanelComm_FillTxBytesWithDebugPack(void);
void PutDebugByteInTxBuf(uint8_t byte, uint16_t offset);
static uint16_t IdxModLen1(uint16_t idx);

void PutCharBuf(char* str, uint8_t len) {
    uint8_t i;
    for (i = 0; i < len; i++) {
        CharBuf[(CharBufTxTail + i) & (CHARBUF_LEN - 1)] = (uint8_t)str[i];
    }
    CharBufTxTail = (CharBufTxTail + len) & (CHARBUF_LEN - 1);
}

void SendDebugStr(void)
{
    uint8_t diffHeadTail;
    if (CharBufTxTail >= CharBufTxHead)
    {
        diffHeadTail = CharBufTxTail - CharBufTxHead;
    }
    else
    {
        diffHeadTail = (CHARBUF_LEN - CharBufTxHead) + CharBufTxTail;
    }

    if (diffHeadTail <= 0)
    {
        return;
    }
    uint8_t len = diffHeadTail > MAX_CHAR_SENT_ONCE_LEN ? MAX_CHAR_SENT_ONCE_LEN : diffHeadTail;

    PanelCommDebugPacket.PackHead = 0xAA;
    PanelCommDebugPacket.PackIndex = 0;
    PanelCommDebugPacket.StationNum = 0xE0;
    PanelCommDebugPacket.Cmd = 0xFA;
    PanelCommDebugPacket.DataByteLength = len;
    PanelCommDebugPacket.PackLength = 7 + len;
    int i;
    for (i = 0; i < len; i++)
    {
        PanelCommDebugPacket.Data[i] = CharBuf[(CharBufTxHead + i) & (CHARBUF_LEN - 1)];
    }
    PanelCommDebugPacket.PackTail = 0xBB;

    PanelComm_FillTxBytesWithDebugPack();

    CharBufTxHead = IdxModLen1(CharBufTxHead + len);
}


void PanelComm_FillTxBytesWithDebugPack(void)
{
    PutDebugByteInTxBuf(PanelCommDebugPacket.PackHead, 0);
    PutDebugByteInTxBuf(PanelCommDebugPacket.PackIndex, 1);
    PutDebugByteInTxBuf(PanelCommDebugPacket.StationNum, 2);
    PutDebugByteInTxBuf(PanelCommDebugPacket.PackLength, 3);
    PutDebugByteInTxBuf(PanelCommDebugPacket.PackLength >> 8, 4);
    PutDebugByteInTxBuf(PanelCommDebugPacket.Cmd, 5);

    uint8_t i;
    for (i = 0; i < PanelCommDebugPacket.DataByteLength; i++)
    {
        PutDebugByteInTxBuf(PanelCommDebugPacket.Data[i], 6 + i);
    }

    uint16_t crc = CRC16_Chk_Tx(IdxModLen1(PanelTxTail + 1), PanelCommDebugPacket.PackLength - 2);
    PutDebugByteInTxBuf(crc, 6 + PanelCommDebugPacket.DataByteLength);
    PutDebugByteInTxBuf(crc >> 8, 6 + PanelCommDebugPacket.DataByteLength + 1);
    PutDebugByteInTxBuf(PanelCommDebugPacket.PackTail, 6 + PanelCommDebugPacket.DataByteLength + 2);

    #if PRINT_CHAR_TO_PANEL_OR_RF
        PanelTxTail = IdxModLen1(PanelTxTail + 7 + PanelCommDebugPacket.DataByteLength + 2);
    #else
        RfTxTail = IdxModLen1(RfTxTail + 7 + PanelCommDebugPacket.DataByteLength + 2);
    #endif
}

void PutDebugByteInTxBuf(uint8_t byte, uint16_t offset) {
    #if PRINT_CHAR_TO_PANEL_OR_RF
        PanelTxData[IdxModLen1(PanelTxTail + offset)] = byte;
    #else
        RfTxData[IdxModLen1(RfTxTail + offset)] = byte;
    #endif
}

static uint16_t IdxModLen1(uint16_t idx) {
    return idx & (CHARBUF_LEN - 1);
}

/*****************************************************************************************************************/
#define M_DataNumber (11*2 + 1)
uint8_t M_chDataDMA[M_DataNumber];

int16_t M_channel0;
int16_t M_channel1;
int16_t M_channel2;
int16_t M_channel3;
int16_t M_channel4;
int16_t M_channel5;
int16_t M_channel6;
int16_t M_channel7;
int16_t M_channel8;
int16_t M_channel9;
int16_t M_channel10;
int16_t M_channel11;
int16_t M_channel12;
int16_t M_channel13;
int16_t M_channel14;

void InitUart_Debug(void)
{
    
    
    DMACONSET = 0x8000;//ON
    DCH0CON = 0x03; //Automatically enable CHAEN = 0, priority CHPRI = 3 CHPATLEN = 0 byte mode
    DCH0ECON = (166 << 8) | 0x10; //UART6 transmission complete

    DCH0SSIZ = M_DataNumber;
    DCH0DSIZ = 1; 
    DCH0CSIZ = 1; //1 byte is transmitted when an event occurs

    
    DCH0INTCLR = 0x00ff00ff;

    IEC2SET = 0U | 0x100U;
       
//    DMAC_ChannelSetAddresses(DMAC_CHANNEL_0 , M_chDataDMA, &U6TXREG);
    
    DCH0CONSET = 0x80;//CHEN = 1
    DCH0ECONSET = 0x80 ;//CFORCE = 1 trigger 
  
   
}
void M_Debug(void)
{
    uint8_t i;
    static uint16_t wCount = 0;

    wCount++;
    if(wCount >= 2) //once 200us 
    {
        wCount = 0;
   
        M_channel0 = 0;
        M_channel1 = 0;
        M_channel2 = 0;
        M_channel3 = 0;
        M_channel4 = 0;
        M_channel5 = 0;
        M_channel6 = 0;
        M_channel7 = 0;
        M_channel8 = 0;
        M_channel9 = 0;
        M_channel10 = 0;        
        
         if((DCH0INT & 0x80) != 0) //Wait for the channel transmission to complete
         {
            DCH0CONCLR = 0x80;    //CHEN = 0           
            DCH0INTCLR = 0x80;    //CFORCE = 0

            M_chDataDMA[0] = M_channel0 >> 8;
            M_chDataDMA[1] = M_channel0;

            M_chDataDMA[2] = M_channel1 >> 8;
            M_chDataDMA[3] = M_channel1;

            M_chDataDMA[4] = M_channel2 >> 8;
            M_chDataDMA[5] = M_channel2;  

            M_chDataDMA[6] = M_channel3 >> 8;
            M_chDataDMA[7] = M_channel3;

            M_chDataDMA[8] = M_channel4 >> 8;
            M_chDataDMA[9] = M_channel4;

            M_chDataDMA[10] = M_channel5 >> 8;
            M_chDataDMA[11] = M_channel5;

            M_chDataDMA[12] = M_channel6 >> 8;
            M_chDataDMA[13] = M_channel6; 

            M_chDataDMA[14] = M_channel7 >> 8;
            M_chDataDMA[15] = M_channel7;

            M_chDataDMA[16] = M_channel8 >> 8;
            M_chDataDMA[17] = M_channel8;

            M_chDataDMA[18] = M_channel9 >> 8;
            M_chDataDMA[19] = M_channel9;

            M_chDataDMA[20] = M_channel10 >> 8;
            M_chDataDMA[21] = M_channel10;        
            
            M_chDataDMA[M_DataNumber - 1] = 0;
                            i = 0;
                            while(i < M_DataNumber)
                            {
                                if(i < M_DataNumber - 1)
                                {
                                    M_chDataDMA[M_DataNumber - 1] += M_chDataDMA[i];//checksum
                                }
                                i++;						
                            } 
                             DCH0CONSET = 0x80;//CHEN = 1
                             DCH0ECONSET = 0x80 ;//CFORCE = 1 trigger 
                 }
    }
}
